<template>
  <div class="dashboard-container">
    <div class="app-container">
      <!-- 头部栏的设计 -->
      <PageTools :show-before="true">
        <!-- 左侧栏 -->
        <span slot="before">共{{ page.total }}条记录</span>
        <template slot="after">
          <el-button size="small" type="warning" @click="$router.push('/import')">导入</el-button>
          <el-button size="small" type="danger" @click="exportExcel">导出</el-button>
          <el-button size="small" type="primary" @click="addemployees">新增员工</el-button>
        </template>
      </PageTools>
      <!-- 表格的制作 -->
      <el-card>
        <el-table :data="list" border="">
          <el-table-column label="序号" type="index" header-align="center" align="center" width="80" />
          <el-table-column label="姓名" sortable header-align="center" align="center" prop="username" />
          <el-table-column label="手机号码" sortable header-align="center" align="center" prop="mobile" />
          <el-table-column label="工号" sortable header-align="center" align="center" prop="workNumber" />
          <el-table-column label="聘用形式" sortable header-align="center" align="center" prop="formOfEmployment" :formatter="formatEmployment" />
          <el-table-column label="部门" sortable header-align="center" align="center" prop="departmentName" />
          <el-table-column label="入职时间" sortable header-align="center" align="center" prop="timeOfEntry">
            <template slot-scope="{row}">{{ row.timeOfEntry | formatDate }}</template>
          </el-table-column>
          <el-table-column label="账户状态" sortable header-align="center" align="center" prop="enableState">
            <template slot-scope="{row}">
              <el-switch :value="row.enableState===1" /></template>
          </el-table-column>
          <!-- 给操作行添加按钮 -->
          <el-table-column label="操作" width="280" header-align="center" align="center">
            <template slot-scope="obj">
              <el-button type="text" size="small" @click="$router.push(`/employees/detail/${obj.row.id}`)">查看</el-button>
              <el-button type="text" size="small">转正</el-button>
              <el-button type="text" size="small">调岗</el-button>
              <el-button type="text" size="small">离职</el-button>
              <el-button type="text" size="small">角色</el-button>
              <el-button type="text" size="small" @click="delEmployee(obj.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!--分页的制作 -->
        <el-row type="flex" justify="center" align="middle" style="height:60px">
          <el-pagination
            layout="prev, pager, next"
            :total="page.total"
            :page-size="page.size"
            :current-page="page.page"
            @current-change="changePage"
          />
        </el-row>
        <!-- 弹层的制作 -->
      </el-card>

    </div>
    <addDept :show-dialog="showDialog" @closeshowDialog="closeshowDialog" />
  </div>
</template>

<script>
// 引入员工的枚举数据
import EmployeeEnum from '@/api/constant/employees'
// 引入获取员工列表的信息
import { getEmployees, delEmployee } from '@/api/employees'
// 引入弹层组件
import addDept from './components/add-dept'
import { formatDate } from '@/filters' // 这么引用是把它当成工具方法来用
export default {
  components: {
    addDept
  },
  data() {
    return {
      list: [], // 用于存储员工全部列表的数组
      page: {
        page: 1,
        size: 10,
        total: 0
      },
      showDialog: false
    }
  },
  // 当页面加载的时候，引用获取到员工列表的方法
  created() {
    this.getEmployees()
  },
  methods: {
    // 获取到所有员工列表
    async getEmployees() {
      const result = await getEmployees(this.page)
      console.log(result)
      this.list = result.rows
      this.page.total = result.total
    },
    // 分页的方法
    changePage(newPage) {
      this.page.page = newPage
      this.getEmployees()
    },
    // 入职时间
    formatEmployment(row) {
      const list = EmployeeEnum.hireType // 枚举列表
      const item = list.find(item => item.id === row.formOfEmployment)
      return item ? item.value : '无'
    },
    // 删除功能
    delEmployee(row) {
      this.$confirm('确定要删除吗').then(() => {
        return delEmployee(row.id)
      }).then(() => {
        this.getEmployees()
      })
    },
    // 点击新增员工
    addemployees() {
      this.showDialog = true
    },
    // 点击关闭弹窗
    closeshowDialog() {
      this.showDialog = false
    },
    // 导出数据
    exportExcel() {
      // 因为在列表中需要进行中英文的切换，所以先定义一个对象
      const headers = {
        '姓名': 'username',
        '手机号': 'mobile',
        '入职日期': 'timeOfEntry',
        '聘用形式': 'formOfEmployment',
        '转正日期': 'correctionTime',
        '工号': 'workNumber',
        '部门': 'departmentName'
      }
      // 导入做导出表格的文件
      import('@/vendor/Export2Excel').then(async(excel) => {
        // 将接口返回的值赋值给返回的rows
        const { rows } = await getEmployees({ page: 1, size: this.page.total })
        const data = this.formatJson(headers, rows)
        excel.export_json_to_excel({
          header: Object.keys(headers),
          data,
          filename: '员工信息表',
          autoWidth: true,
          bookType: 'xlsx'
        })
      })
    },
    formatJson(headers, rows) {
      // 首先遍历数组
      // [{ username: '张三'},{},{}]  => [[’张三'],[],[]]
      return rows.map(item => {
        return Object.keys(headers).map(key => {
          if (headers[key] === 'timeOfEntry' || headers[key] === 'correctionTime') {
            return formatDate(item[headers[key]]) // 返回格式化之前的时间
          } else if (headers[key] === 'formOfEmployment') {
            var en = EmployeeEnum.hireType.find(obj => obj.id === item[headers[key]])
            return en ? en.value : '未知'
          }
          return item[headers[key]]
        }) // => ["张三", "13811"，"2018","1", "2018", "10002"]
      })
      // return data
      // return rows.map(item => {
      //   // item是对象  => 转化成只有值的数组 => 数组值的顺序依赖headers  {username: '张三'  }
      //   // Object.keys(headers)  => ["姓名", "手机号",...]
      //   return Object.keys(headers).map(key => {
      //     return item[headers[key]]
      //   }) // /  得到 ['张三'，’129‘，’dd‘,'dd']
      // })
    }
  }
}
</script>

<style>

</style>
